
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@janiscommerce/database-dispatcher
Advanced tools
DatabaseDispatcher is a package that returns the necessary DB driver from a received model. Access to the databases configuration then returns the driver instance with the connection. It caches the driver per config properties.
npm install @janiscommerce/database-dispatcher
# Then install all the drivers that you need.
npm install --save @janiscommerce/mysql
npm install --save @janiscommerce/mongodb
npm install --save @janiscommerce/elasticsearch
# or any other DBDriver if it's exists
Config file with Settings package, with settings in JSON file /path/to/root/.janiscommercerc.json
into the key database
, clients
, databaseWriteType
, databaseReadType
:
{
"database": {
"core": {
"type": "mysql",
"host": "http://my-host-name.com",
"database": "my-database"
// ...
},
"other": {
"type": "mongodb",
"host": "http://other-host-name.com",
"database": "my-database"
// ...
},
"another": {
"type": "elasticsearch",
"host": "http://another-host-name.com",
// ...
}
},
"databaseWriteType": "mongodb",
"databaseReadType": "mysql",
"clients": {
"database": {
"fields": {
"write": {
"dbHost": "host",
"database": "collection"
},
"read": {
"url": "host",
"dbDatabase": "database"
}
}
}
}
}
type
: if it's not exist, databaseWriteType
will be use as default.To assign Read and Write types of DBDrivers.
databaseWriteType
: Required, Type of Write Database. It's the default DBDriver for every database.databaseReadType
: Optional, Type of Read Database.clients.database.fields.write
: For Write DBclients.database.fields.read
: For Read DBkey
: Field present in Client objectvalue
: Field needed in DBDriverIn .janiscommercerc.json
{
"clients": {
"database": {
"fields": {
"write": {
"url": "host",
"index": "database"
}
}
}
},
"databaseWriteType": "elasticsearch"
}
Client Object getted from Client DB
client = {
id: '123'
name: 'Company',
url: 'http://company-host-name.com',
index: 'company.index'
}
Config will be
config = {
host: 'http://company-host-name.com',
databse: 'company.index'
}
getDatabaseByKey(databaseKey)
Receives the database key [String]
and returns the database driver instance associeted to a config.
If the databaseKey
dosen't exists in any config source will throw a DatabaseDispatcherError
.
The default value of databaseKey
parameter is "_default"
.
getDatabaseByConfig(config)
Receives the config [Object]
and returns the database driver instance.
getDatabaseByClient(clientObject, useReadDB)
Receives the client object [Object]
and returns the database driver instance associeted to a config.
If useReadDB
([Boolean]
) doesn't exists or it's FALSE
will try to get Write DBDriver Type by default, if it's TRUE
will try to get Read DBDriver Type.
If the databaseReadType
doesn't exists will use databaseWriteType
.
If the databaseWriteType
doesn't exists will be throw a DatabaseDispatcherError
.
clearCache() Clear the internal cache, including config and DB connections.
The errors are informed with a DatabaseDispatcherError
.
This object has a code that can be useful for a correct error handling.
The codes are the following:
Code | Description |
---|---|
1 | Settings not found |
2 | Invalid settings |
3 | ConfigDB not found for databaseKey in Settings |
4 | Invald ConfigDB found in Settings for a databaseKey |
5 | Invalid Client Object |
6 | DB Driver not installed |
7 | Invalid DB Driver (not a valid Class) |
const DatabaseDispatcher = require('@janiscommerce/database-dispatcher');
/*
/path/to/.jannsicommercerc.json
database: {
core: {
type: 'mysql',
host: 'foo',
...
},
...
},
databaseWriteType: 'mongodb',
databaseReadType: 'elasticsearch',
clients: {
database: {
fields: {
write: {
dbHost: 'host'
},
read: {
url: 'host',
index: 'database'
}
}
}
},
...
*/
// Get by Key
const myDBConnection = DatabaseDispatcher.getDatabaseByKey('core'); // A new DBDriver instance is returned.
console.log(myDBConnection); // expected output: DBDriver
// Get Write DB by Client
const client = model.getClientById('123') // Or Any method you use to get client's objects
const myDBWriteConnection = DatabaseDispatcher.getDatabaseByClient(client); // A new DBDriver instance is returned.
console.log(myDBWriteConnection.constructor.name); // expected output: MongoDB
// Get Read DB by Client
const otherClient = model.getClientById('1234') // Or Any method you use to get client's objects
const myDBWriteConnection = DatabaseDispatcher.getDatabaseByClient(otherClient, true); // A new DBDriver instance is returned.
console.log(myDBWriteConnection.constructor.name); // expected output: ElasticSearch
DatabaseDispatcher.clearCache(); // cached connections and configs cleared.
FAQs
Database dispatcher
We found that @janiscommerce/database-dispatcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.